//
//  HYBroadCastViewModel.m
//  XPlayer
//
//  Created by 张水 on 2023/10/25.
//

#import "HYFaceToFaceViewModel.h"
#import "HYVideoModel.h"
#import "HYVideoPlayerConfig.h"
#import "HYVideoViewModel.h"

@interface HYFaceToFaceViewModel()

@property (nonatomic, strong) NSMutableArray *dataSoucre;

@property (nonatomic, strong) NSMutableArray *modelArray;

@end

@implementation HYFaceToFaceViewModel

- (instancetype)init
{
    if (self = [super init]) {
        self.dataSoucre = [NSMutableArray array];
        self.modelArray = [NSMutableArray array];
        self.playSubject = [RACSubject subject];
        self.jumpSubJect = [RACSubject subject];
        self.refreshSubJect = [RACSubject subject];
        [self bindSignal];
    }
    return self;
}
- (void)bindSignal
{
    @weakify(self);
    [self.updatedContentSignal subscribeNext:^(HYHomeFileItemViewModel *videoModel) {
        @strongify(self);
        [self.modelArray hy_addObject:videoModel];
        NSDictionary *downloadingDic = @{@"title":NSLocalizedString(@"已传送文件", nil),@"itemSource":self.modelArray};
        [self.dataSoucre replaceObjectAtIndex:1 withObject:downloadingDic];
        [self.refreshSubJect sendNext:@""];
    }];
}
- (void)loadData
{
    NSDictionary *tipsDic = @{@"title":NSLocalizedString(@"双方都打开此界面才可以传送", nil),@"itemSource":@[@1]};
    NSDictionary *downloadingDic = @{@"title":NSLocalizedString(@"已传送文件", nil),@"itemSource":@[]};
    [self.dataSoucre hy_addObject:tipsDic];
    [self.dataSoucre hy_addObject:downloadingDic];
}

- (NSInteger)numberOfSectionsInTableView
{
    return self.dataSoucre.count;
}

- (HYHomeFileType)fetchFileType:(NSIndexPath *)indexPath
{
    NSDictionary *sectionInfo = self.dataSoucre[indexPath.section];
    NSArray *itemSource = sectionInfo[@"itemSource"];
    HYHomeFileItemViewModel *fileModel = itemSource[indexPath.row];
    return fileModel.fileType;
}

- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *sectionInfo = self.dataSoucre[section];
    return [sectionInfo[@"itemSource"] count];
}

- (NSString *)headerTitleForHeaderInSection:(NSInteger)section
{
    NSDictionary *sectionInfo = self.dataSoucre[section];
    return sectionInfo[@"title"];
}
- (id)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *sectionInfo = self.dataSoucre[indexPath.section];
    NSArray *itemSource = sectionInfo[@"itemSource"];
    return itemSource[indexPath.row];
}

- (void)didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        [self.jumpSubJect sendNext:@""];
    }else{
        NSDictionary *sectionInfo = self.dataSoucre[indexPath.section];
        NSArray *itemSource = sectionInfo[@"itemSource"];
        HYHomeFileItemViewModel *itemViewModel = itemSource[indexPath.row];
        [self.playSubject sendNext:itemViewModel];
    }
}

- (HYVideoViewModel *)fetchPlayVideoConfigWithItemViewModel:(HYHomeFileItemViewModel *)fileModel
{
    HYVideoModel *currentVideo = [[HYVideoModel alloc]init];
    currentVideo.videoTitle = fileModel.fileName;
    currentVideo.videoUrl = fileModel.filePath;
    currentVideo.videoImage = fileModel.videoImage;
    currentVideo.isAudio = fileModel.fileType;
    currentVideo.createDate = fileModel.createDate;
    currentVideo.importDate = fileModel.importDate;
    currentVideo.isSelect = YES;
    HYVideoPlayerConfig *config = [[HYVideoPlayerConfig alloc]init];
    config.isBasePlayer = YES;
    config.currentVideoModel = currentVideo;
    config.videoSoucres = @[currentVideo];
    HYVideoViewModel *videoModel = [[HYVideoViewModel alloc]initWithFolderIdentification:fileModel.folderIdentification isPrivate:fileModel.isSuperPrivate videoConfig:config];
    return videoModel;
}

- (CGFloat)heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 65;
    }
    if (indexPath.section == 1) {
        return kItemListHeight+10;
    }
    return 0.01;
}
@end
